<?php

namespace App\ZhCN;

if (!defined('IN_PX'))
    exit;

use App\Service;
use App\Repository;
use App\Tools\Auxi;
use App\Tools\UrlHelper;
use App\Tools\Spell;
use Phoenix\Log\Log4p as logger;

/**
 * 首页
 */
class Index {

    private function __Controller() {}

    private function __Value($__PACKAGE__, $__ROOT__, $__RM__, $setting, $cfg, $__LANGUAGE_ID__) {}

    protected function __Inject($session, Service\Archives $servArc, Repository\Archives $repoArc,
                                Repository\Category $category) {}

    public function index() {
        if ($this->servArc->chkCategoryDataView()) {

            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            $industry_key   =    self::get_key_by_channel_type($_model['aryCategoryDataView'], 2);
            $_model['aliasId1'] = 'home';
            $_model['battery_content']      =   $_model['aryCategoryDataView'][$industry_key];
            $_model['aryAd'] = $this->repoArc->getAd($this->__LANGUAGE_ID__);
            $_model['homeCase'] = $this->repoArc->findHome(2, $this->__LANGUAGE_ID__, 0, 6);
            $_model['homeProduct'] = $this->repoArc->findHome(1, $this->__LANGUAGE_ID__, 0, 6);
            $_model['homeNews'] = $this->repoArc->findHome(3, $this->__LANGUAGE_ID__, 0, 4);
            $_model['case'] = $this->repoArc->findCase(2, $this->__LANGUAGE_ID__);
//            $_model['pics'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 1);
            return array (
                'model' => $_model,
                'view' => true
            );
        }
        return 404;
    }

    public function category($__Route = array('/*/{aliasId}/{page:\d*}', '/category/{aliasId}/{page}'))
    {
        if (!is_null(($_model = $this->servArc->getCategoryRs($this->aliasId, true)))) {
            $_model['aliasId'] = $this->aliasId;
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;
            if (!isset($this->page)) {
                $this->page = 1;
            }
            $_model['currentPage'] = $this->page;

            //根目录下的总条数
            $_model['rootCategoryTotal'] = $this->servArc->findAllRootCount($_model['rootId']);

            if (in_array($_model['channelType'], array(0,2,3,5,6,7,9,10,11,14,15))) { ///走进骄成
                if ($_model['categoryLevel'] == 1) {
                    $_model['categoryId'] = self::get_key($_model['aryCategoryDataView'][$_model['rootId']]);
                }
                $_model['product_column'] = $_model['aryCategoryDataView'][$_model['rootId']];
                $_model['content'] = $_model['product_column'][$_model['categoryId']]['substance'];
                if (in_array($_model['channelType'], array(2,3,5,7,14,15))) {
                    $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                        $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                        $_model['currentPageSize']);
                }
                if (in_array($_model['channelType'], array(2))) {
                    $_model['product_info'] =   $_model['product_column'][$_model['categoryId']];
                }
                if (in_array($_model['channelType'], array(3))) {
                    $_model['currentCategoryTotal'] = $this->repoArc->count($_model['categoryId']);
                }
            } else if ($_model['channelType'] == 1) {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);

                $_model['product_column'] = $_model['aryCategoryDataView'][$_model['rootId']];
                if ($_model['categoryLevel'] == 1) {
                    ///左侧竖排导航
                    if ($_model['aryCategoryDataView'][$_model['rootId']]) {
                        $i = $m = 0; $first_name_arr= array();
                        foreach ($_model['aryCategoryDataView'][$_model['rootId']] as $key => $val) {
                            if ($i == 0){
                                $i++;
                                $_model['product_category']= $val;
                                $first_name_arr =  $_model['aryCategoryDataView'][$_model['rootId']][$key];
                            } else {
                                break;
                            }
                        }
                        ///右侧显示的标题和产品列表；
                        if ($first_name_arr && is_array($first_name_arr)) {
                            foreach ($first_name_arr as $k => $v) {
                                if ($m == 0) {
                                    $m++;
                                    $now_category_name  = $v['category_name'];
                                    $now_category_id    = $k;
                                }
                            }
                        }
                    }
                } elseif ($_model['categoryLevel'] == 2) {
                    ///左侧竖排导航
                    $_model['product_category'] = $_model['aryCategoryDataView'][$_model['parentId']][$_model['categoryId']];
                    ///右侧显示的标题和产品列表；
                    $first_name_arr = $_model['aryCategoryDataView'][$_model['parentId']][$_model['categoryId']];
                    if ($first_name_arr && is_array($first_name_arr)) {
                        $m = 0;
                        foreach ($first_name_arr as $k => $v) {
                            if ($m == 0) {
                                $m++;
                                $now_category_name  = isset($v['category_name']) ? $v['category_name'] : $first_name_arr['category_name'];
                                $now_category_id    = $k;
                            }
                        }
                    }
                } elseif ($_model['categoryLevel'] == 3) {
                    ///左侧竖排导航
                    $_model['product_category'] = $_model['aryCategoryDataView'][$_model['rootId']][$_model['parentId']];
                    ///右侧显示的标题和产品列表；
                    $now_category_name  = $_model['categoryName'];
                    $now_category_id    = $_model['categoryId'];
                }
                $_model['now_category_name'] = $now_category_name;
                $_model['products'] = $this->servArc->findAll($now_category_id,
                $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                $_model['currentPageSize']);
                $_model['currentCategoryTotal'] = $this->repoArc->count($now_category_id);
            } elseif ($_model['channelType'] == 12) {
                $title  = $_GET['title'];
                $result = $this->servArc->findTitle($title);
                $_model['search'] = $result;
            }  elseif (in_array($_model['channelType'], array(13))) {
                if ($_model['categoryLevel'] == 1) {
                    $_model['categoryId'] = self::get_key($_model['aryCategoryDataView'][$_model['rootId']]);
                }
                $_model['product_column'] = $_model['aryCategoryDataView'][$_model['rootId']];
                $_model['pics_top'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 2);
                $_model['pics_bottom'] = $this->repoArc->getAd($this->__LANGUAGE_ID__, 3);
            } else {
                $_model['currentListRs'] = $this->servArc->findAll($_model['categoryId'],
                    $_model['currentPageSize'] * ($_model['currentPage'] - 1),
                    $_model['currentPageSize']);
            }
            $_model['categoryName'] = $this->repoArc->findCategory($_model['categoryId']);
            $_model['pics'] = $this->repoArc->productListPics($_model['categoryId']);
            $_model['isMobile'] = UrlHelper::pdMobileUrl();
            return array(
                'model' => $_model,
                'view' => $this->setting['aryChannelTypeMapping'][$this->__PACKAGE__][$_model['channelType']][1]
            );
        }
        return 404;
    }

    public function detail($__Route = array('/{id:\d+}', '/product/detail/{id:\d+}', '/project/detail/{id:\d+}', '/news/detail/{id:\d+}', '/case/detail/{id:\d+}')) {
        if (!is_null(($_model = $this->servArc->getSubstance($this->id)))) {
            $parent = explode('.', trim($_model['idTree'], '.'));
            $_model['parentId'] = $parent[1];
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

            if ($_model['channelType'] == 1) { //产品多图
                $_model['pics'] = $this->repoArc->productPics($this->id);
                foreach ($_model['aryCategoryDataView'][$_model['rootId']] as $key => $val) {
                     if(isset($val['category_name'])){
                         $product[$key]['category_name']    =   $val['category_name'];
                         $product[$key]['list_dir']         =   $val['list_dir'];
                         $product[$key]['id_tree']         =   $val['id_tree'];
                     }
                }
                $_model['product_category'] = $product;
            }
//            var_dump($_model['pics']);die;

            return array(
                'model' => $_model,
                'view' => $this->setting['aryChannelTypeMapping'][$this->__PACKAGE__][$_model['channelType']][3]
            );
        }
        return 404;
    }

    public function searchDetail($__Route = array('/search/detail/{id:\d+}')) {
        if (!is_null(($_model = $this->servArc->getSubstance($this->id)))) {
            $parent = explode('.', trim($_model['idTree'], '.'));
            $_model['parentId'] = $parent[1];
            $_model['aryCategoryDataView'] = $this->servArc->aryCategoryDataView;

            if ($_model['channelType'] == 1) { //产品多图
                $_model['pics'] = $this->repoArc->productPics($this->id);
                foreach ($_model['aryCategoryDataView'][$_model['rootId']] as $key => $val) {
                    if(isset($val['category_name'])){
                        $product[$key]['category_name']    =   $val['category_name'];
                        $product[$key]['list_dir']         =   $val['list_dir'];
                    }
                }
                $_model['product_category'] = $product;
            }

            return array(
                'model' => $_model,
                'view' => 'search/detail'
            );
        }
        return 404;
    }

    ////得到分类下的第一个子栏目的Key
    public function get_key($first_name_arr){
        $key = 0; $m = 0;
        if ($first_name_arr && is_array($first_name_arr)) {
            foreach ($first_name_arr as $k => $v) {
                if ($m == 0) {
                    $m++;
                    $key    = $k;
                }
            }
        }
        return $key;
    }

    ///根据channel_type来得到root_id;
    public function get_key_by_channel_type($_arr, $channel_type){
        $_new_key = 0;
        if ($_arr && is_array($_arr)) {
            foreach($_arr as $key => $val) {
                if ($val['channel_type'] == $channel_type) {
                    $_new_key = $key;
                }
            }
        }
        return $_new_key;
    }
}
